package com.bcbim.operation_maintenance.space_category.service;

import com.bcbim.common.sys.Constant;
import com.bcbim.common.util.GuidUtil;
import com.bcbim.operation_maintenance.space_category.mapper.SpaceCategoryMapper;
import com.bcbim.operation_maintenance.space_type.mapper.SpaceTypeMapper;
import com.bcbim.operation_maintenance_interface.space.dto.SpaceDTO;
import com.bcbim.operation_maintenance_interface.space_category.dao.SpaceCategoryDAO;
import com.bcbim.operation_maintenance_interface.space_category.service.SpaceCategoryService;
import com.bcbim.operation_maintenance_interface.space_category.vo.ListSpaceCategoryTreeVO;
import com.bcbim.operation_maintenance_interface.space_category.vo.ListSpaceCategoryVO;
import com.bcbim.operation_maintenance_interface.space_category.vo.SpaceCategoryTreeVO;
import com.bcbim.operation_maintenance_interface.space_category.vo.SpaceCategoryVO;
import com.bcbim.operation_maintenance_interface.space_type.vo.ListSpaceTypeVO;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @version V 1.0.0
 * @Description：空间类别service类实现层
 * @Author: haomeijie
 * @Date: 2022/3/4 17:41
 **/
@Service(timeout = 60 * 1000, group = "spaceCategory", version = "1.0.0")
public class SpaceCategoryServiceImpl implements SpaceCategoryService {

    @Autowired
    private SpaceCategoryMapper spaceCategoryMapper;
    @Autowired
    private SpaceTypeMapper spaceTypeMapper;

    @Override
    public int insertSpaceCategory(SpaceCategoryDAO spaceCategoryDAO) {
        return spaceCategoryMapper.insertSpaceCategory(spaceCategoryDAO);
    }

    @Override
    public int updateSpaceCategory(SpaceCategoryDAO spaceCategoryDAO) {
        return spaceCategoryMapper.updateSpaceCategory(spaceCategoryDAO);
    }

    @Override
    public SpaceCategoryVO getSpaceCategory(String id) {
        return spaceCategoryMapper.getSpaceCategory(id);
    }

    @Override
    public List<SpaceCategoryTreeVO> listSpaceCategoryTree(String projectId) {
        List<SpaceCategoryTreeVO> list = spaceCategoryMapper.listSpaceCategoryTree(projectId);
        List<SpaceCategoryTreeVO> parentList = list.stream()
                .filter(spaceCategoryTreeVO -> StringUtils.isEmpty(spaceCategoryTreeVO.getParentId()))
                .collect(Collectors.toList());
        buildTree(list, parentList);
        return parentList;
    }

    @Override
    public List<ListSpaceCategoryVO> listSpaceCategory(String name, String projectId) {
        List<ListSpaceCategoryVO> list = spaceCategoryMapper.listSpaceCategory(name, projectId);
        List<ListSpaceCategoryVO> typeList = spaceTypeMapper.listSpaceType(name, projectId);
        List<ListSpaceCategoryVO> childList;
        for (ListSpaceCategoryVO listSpaceCategoryVO : list) {
            childList = new ArrayList<>();
            listSpaceCategoryVO.setSign("1");
            for (ListSpaceCategoryVO type : typeList) {
                if (type.getParentId().equals(listSpaceCategoryVO.getId())) {
                    type.setSign("2");
                    childList.add(type);
                }
            }
            if (ObjectUtils.isEmpty(childList)) {
                childList = null;
            }
            listSpaceCategoryVO.setChildList(childList);
        }
        return list;
    }


    @Override
    public List<ListSpaceCategoryTreeVO> listCategoryType(SpaceDTO spaceDTO) {
        //查询类别-类型列表
        List<ListSpaceCategoryTreeVO> list = spaceCategoryMapper.listCategory(spaceDTO);
        //根据条件查询类别-类型列表
        List<ListSpaceCategoryTreeVO> categoryTree = spaceCategoryMapper.listCategoryByQuery(spaceDTO);
        if (ObjectUtils.isEmpty(spaceDTO.getFloorIds()) && StringUtils.isEmpty(spaceDTO.getSpaceTypeJson())
                && StringUtils.isEmpty(spaceDTO.getDepartmentJson()) && StringUtils.isEmpty(spaceDTO.getSpaceNumber())
                && StringUtils.isEmpty(spaceDTO.getSpaceName()) && !spaceDTO.getIsBlank()) {
            List<String> id = new ArrayList<>();
            categoryTree.stream().forEach(c -> id.add(c.getId()));
            list = list.stream().filter(space -> !id.contains(space.getId())).collect(Collectors.toList());
            categoryTree.addAll(list);
        }
        List<ListSpaceCategoryTreeVO> categoryList = categoryTree.stream()
                .filter(c -> c.getSign().equals("1")).collect(Collectors.toList());
        //根据名称进行排序
        Collections.sort(categoryList, new Comparator<ListSpaceCategoryTreeVO>() {
            @Override
            public int compare(ListSpaceCategoryTreeVO o1, ListSpaceCategoryTreeVO o2) {
                //排序规则：按照汉字拼音首字母排序
                Comparator<Object> com = Collator.getInstance(java.util.Locale.CHINA);
                return com.compare(o1.getSpaceCategory(), o2.getSpaceCategory());
            }
        });
        //未指派放首位
        for (int i = 0; i < categoryList.size(); i++) {
            ListSpaceCategoryTreeVO c = categoryList.get(i);
            if (c.getSpaceCategory().equals(Constant.UNASSIGNED)) {
                categoryList.remove(i);
                categoryList.add(0, c);
                continue;
            }
        }
        List<ListSpaceCategoryTreeVO> typeList = categoryTree.stream()
                .filter(c -> c.getSign().equals("2")).collect(Collectors.toList());
        //根据名称进行排序
        //根据名称排序
        Collections.sort(typeList, new Comparator<ListSpaceCategoryTreeVO>() {
            @Override
            public int compare(ListSpaceCategoryTreeVO o1, ListSpaceCategoryTreeVO o2) {
                //排序规则：按照汉字拼音首字母排序
                Comparator<Object> com = Collator.getInstance(java.util.Locale.CHINA);
                return com.compare(o1.getSpaceType(), o2.getSpaceType());
            }
        });
        //未指派放首位
        for (int i = 0; i < typeList.size(); i++) {
            ListSpaceCategoryTreeVO t = typeList.get(i);
            if (t.getSpaceType().equals(Constant.UNASSIGNED)) {
                typeList.remove(i);
                typeList.add(0, t);
                continue;
            }
        }
        for (ListSpaceCategoryTreeVO category : categoryList) {
            List<ListSpaceCategoryTreeVO> childList = typeList.stream()
                    .filter(type -> type.getSpaceCategoryId().equals(category.getId())).collect(Collectors.toList());
            if (ObjectUtils.isEmpty(childList)) {
                childList = null;
            }
            category.setChildList(childList);
        }
        return categoryList;
    }

    @Override
    public String getUnassignedId(String unassignedName, String projectId) {
        return spaceCategoryMapper.getUnassignedId(unassignedName, projectId);
    }

    @Override
    public Boolean checkCategoryExitStaff(String spaceCategoryId) {
        return spaceCategoryMapper.checkCategoryExitStaff(spaceCategoryId) > 0 ? true : false;
    }

    /**
     * 组件树形结构
     */
    private void buildTree(List<SpaceCategoryTreeVO> list, List<SpaceCategoryTreeVO> parentList) {
        List<SpaceCategoryTreeVO> childList = null;
        for (SpaceCategoryTreeVO spaceCategoryTreeVO : parentList) {
            childList = new ArrayList<>();
            for (SpaceCategoryTreeVO spaceCategoryTree : list) {
                if (StringUtils.isNotEmpty(spaceCategoryTree.getParentId()) && spaceCategoryTree.getParentId().equals(spaceCategoryTreeVO.getId())) {
                    childList.add(spaceCategoryTree);
                }
            }
            if (ObjectUtils.isEmpty(childList)) {
                childList = null;
            }
            spaceCategoryTreeVO.setChild(childList);
        }
        if (ObjectUtils.isNotEmpty(childList)) {
            buildTree(list, childList);
        }
    }

    /**
     * 检查空间类别名称是否重复
     *
     * @return List<SpaceCategoryTreeVO>
     * @Author: xuehao
     * @Date: 2022/3/8 17:13
     */
    @Override
    public int checkSpaceCategory(String spaceCategory, String projectId, String id) {
        return spaceCategoryMapper.checkSpaceCategory(spaceCategory, projectId, id);
    }
}
